package com.hrms.hrms_workflow.controller;

import com.hrms.common.vo.ResultMessage;
import com.hrms.hrms_workflow.constant.FLOW_KEY;
import com.hrms.hrms_workflow.constant.FLOW_RESOURCE;
import com.hrms.hrms_workflow.util.WorkFlowUtil;
import com.hrms.hrms_workflow.vo.ApprovalDetailDTO;
import com.hrms.hrms_workflow.vo.HistoryTaskVO;
import com.hrms.hrms_workflow.vo.TaskVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/recruit")
public class RecruitController {

    @Autowired
    private WorkFlowUtil workFlowUtil;


    @PostMapping("/load")
    public ResultMessage load(){
        //部署流程
        String workLoads = workFlowUtil.workLoads(FLOW_RESOURCE.RECRUIT.getCode(), FLOW_RESOURCE.RECRUIT.getName());
//        String startflow = workFlowUtil.startflow(FLOW_KEY.RECRUIT.getCode(),variables);
        return new ResultMessage(ResultMessage.Success,"",workLoads);
    }

    @PostMapping("/start")
    public ResultMessage recruitStart(@RequestBody Map<String,Object> variables){
        //无需重新部署流程
//        workFlowUtil.workLoads(FLOW_RESOURCE.RECRUIT.getCode(), FLOW_RESOURCE.RECRUIT.getName());
        String startflow = workFlowUtil.startflow(FLOW_KEY.RECRUIT.getCode(),variables);
        return new ResultMessage(ResultMessage.Success,"",startflow);
    }

    @PostMapping("/complete")
    public ResultMessage complete(@RequestParam String taskId, @RequestBody Map<String,Object> variables){
        workFlowUtil.completeflow(taskId,variables);
        return new ResultMessage(ResultMessage.Success,"");
    }

    /**
     * 获取用户所有需要审批的流程
     */
    @GetMapping("/assignee")
    public ResultMessage findFlow(@RequestParam String name) {
        List<TaskVO> list = workFlowUtil.findCurrentFlowByAssignee(name);
        return new ResultMessage(ResultMessage.Success,"",list);
    }
    /**
     * processInstanceId获取流程实例当前运行中的任务
     */
    @GetMapping ("/processInstanceId")
    public ResultMessage findFlowTask(@RequestParam String processInstanceId){
        List<TaskVO> taskList = workFlowUtil.findCurrentFlowByProcessInsId(processInstanceId);
        return new ResultMessage(ResultMessage.Success,"",taskList);
    }

    /**
     * 获取历史数据
     */
    @GetMapping("history")
    private ResultMessage finfFlowHistory(@RequestParam String processInstanceId) {
        List<HistoryTaskVO> historyTaskVOS = workFlowUtil.finfHistory(processInstanceId);
        return new ResultMessage(ResultMessage.Success,"",historyTaskVOS);
    }

    /**
     * 获取流程实例全部数据
     */
    @GetMapping("approvalDetail")
    private ResultMessage approvalDetail(@RequestParam String processInstanceId) {
        ApprovalDetailDTO approvalDetail = workFlowUtil.approvalDetail(processInstanceId);
        return new ResultMessage(ResultMessage.Success,"",approvalDetail);
    }

    /**
     * 获取流程实例xml
     */
    @GetMapping("processDefinitionXml")
    private ResultMessage getProcessDefinitionXml(@RequestParam String processDefinitionId) {
        String xml = workFlowUtil.getProcessDefinitionXml(processDefinitionId);
        return new ResultMessage(ResultMessage.Success,"",xml);
    }

    /**
     * 根据流程定义ID查询所有流程实例
     */
    @GetMapping("/processDefinitionKey")
    public void getProcessInstancesByProcessDefinitionKey(@RequestParam String processDefinitionKey) {
        // 查询指定流程定义下的所有流程实例
        workFlowUtil.getProcessInstancesByProcessDefinitionKey(processDefinitionKey);
        return ;
    }

}
